package control;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import javax.management.Attribute;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.InvalidAttributeValueException;
import javax.management.MBeanException;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;


public class CassandraControlClient {
	
	@SuppressWarnings("deprecation")
	public static void main(String[] args) throws InterruptedException, MalformedObjectNameException, AttributeNotFoundException, InstanceNotFoundException, NullPointerException, MBeanException, ReflectionException, IOException, InvalidAttributeValueException {
		// provide a valid username and password (e.g., via program arguments)
	   
	    String[] ips=new String[]{"192.168.3.228","192.168.3.229","192.168.3.230","192.168.3.231","192.168.3.232"};
	    //"service:jmx:rmi:///jndi/rmi://192.168.3.36:9999/jmxrmi"
	    // place the username and password in a string array of credentials that
	    // can be used when making the connection to the remote JMX agent
	    //11 点10分-->50。12:10分-->40 。 
	    //20 30  
	    int[] threads={8,16,20,24,28,32};
	    Date[] dates={
	    		new Date(113, 10, 17, 03, 21, 0),
	    		new Date(113, 10, 18, 01, 41, 0),//18-01-40的将是8线程的日志
	    		new Date(113, 10, 18, 02, 01, 0),
	    		new Date(113, 10, 18, 02, 21, 0),
	    		new Date(113, 10, 18, 02, 41, 0),
	    		new Date(113, 10, 18, 03, 01, 0),
	    		
	    };
	    assert dates.length==threads.length;
	    File file=new File("logs");
	    if(!file.exists()){
	    	file.mkdirs();
	    }
	    final CassandraMonitor[] clients=new CassandraMonitor[5];//修改速度
	    final TimerTask[][] tasks=new TimerTask[clients.length][threads.length];//每组的前3个用来控制速度（绑定clients），第四个用来打印速度（绑定secondClient）
	    final Timer timer=new Timer();
	    for(int i=0;i<clients.length;i++){
	    	clients[i]=new CassandraMonitor(ips[i],7199,"","");
	    	tasks[i]=new TimerTask[threads.length];
	    	for(int j=0;j<tasks[i].length;j++){
	    		tasks[i][j]=new CassandraMonitor.CassandraTimerTask(clients[i],threads[j]);
	    		//设定触发时间
		    	timer.schedule(tasks[i][j], dates[j]);
		    	System.out.println(ips[i]+" will set thread "+threads[j]+" at "+ dates[j]);
	    	}
	    	
	    }
	    
	    timer.schedule(new TimerTask() {
			@Override
			public void run() {
				for(int i=0;i<clients.length;i++){
					try {
						clients[i].close();
					} catch (IOException e) {
						e.printStackTrace();
					}finally{
						timer.cancel();
					}
				}
			}
		}, new Date(113, 10, 18, 5, 15, 0));
//	    
	}
}
